Removing an old, cherished, yet pointless caveat "This documentation is
[supercollider.git] / Help / Streams-Patterns-Events / A Practical Guide / PG_02_Basic_Vocabulary.html
blobf23952aefe6ee5f5d3f0f98736349ea77d3ecfeb
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.48">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #000000; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
20 p.p11 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #007300}
21 p.p12 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0000bf}
22 p.p13 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #bf0000}
23 p.p14 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000bf}
24 span.s1 {color: #bf0000}
25 span.s2 {color: #0000bf}
26 span.s3 {color: #000000}
27 span.s4 {text-decoration: underline ; color: #0000bf}
28 span.s5 {color: #0000bf}
29 span.s6 {color: #007300}
30 span.s7 {font: 12.0px Helvetica}
31 span.Apple-tab-span {white-space:pre}
32 </style>
33 </head>
34 <body>
35 <p class="p1"><b>Basic Vocabulary: Generating values</b></p>
36 <p class="p2"><br></p>
37 <p class="p3">Before getting to the really cool things patterns can do, we need to build up a basic vocabulary. We'll start with some words, then move into phrases in the next tutorial.</p>
38 <p class="p2"><br></p>
39 <p class="p3">Some of the patterns will be demonstrated with a Pbind construct. This is a taste of things to come -- sequencing sonic events using patterns. Don't worry about how Pbind works just yet... all in good time.</p>
40 <p class="p2"><br></p>
41 <p class="p3">Let's start with a very quick reference of some basic patterns. More complete descriptions follow this list. The list might seem long at first, but concentrate your attention on patterns marked with a star. Those are the most basic, and commonly used. Again, the purpose is to start learning the vocabulary of patterns -- like learning new words when studying a human language.</p>
42 <p class="p2"><br></p>
43 <p class="p3">This document describes a lot of patterns, but what I call "primary patterns" are the most important. If you are new to patterns, concentrate on these first. You can always come back and look at the rest later.</p>
44 <p class="p2"><br></p>
45 <p class="p3">For more information on any of these patterns, select the class name and use the help key for your editor to open its help file.</p>
46 <p class="p2"><br></p>
47 <p class="p2"><br></p>
48 <p class="p4"><b>Quick reference</b></p>
49 <p class="p2"><br></p>
50 <p class="p3"><b>Primary Patterns</b></p>
51 <p class="p2"><br></p>
52 <ul>
53 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pseq(list, repeats, offset):</b> Play through the entire list 'repeats' times. Like list.do.</li>
54 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Prand(list, repeats):</b> Choose items from the list randomly (same as list.choose).</li>
55 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pxrand(list, repeats):</b> Choose randomly, but never repeat the same item twice.</li>
56 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pshuf(list, repeats):</b> Shuffle the list in random order, and use the same random order 'repeats' times. Like list.scramble.</li>
57 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pwrand(list, weights, repeats):</b> Choose randomly by weighted probabilities (like list.wchoose(weights)).</li>
58 </ul>
59 <p class="p2"><br></p>
60 <ul>
61 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pseries(start, step, length):</b> Arithmetic series (addition).</li>
62 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pgeom(start, grow, length):</b> Geometric series (multiplication).</li>
63 </ul>
64 <p class="p2"><br></p>
65 <ul>
66 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pwhite(lo, hi, length):</b> Random numbers, equal distribution ("white noise"). Like rrand(lo, hi).</li>
67 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pexprand(lo, hi, length):</b> Random numbers, exponential distribution. Like exprand(lo, hi).</li>
68 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pbrown(lo, hi, step, length):</b> Brownian motion, arithmetic scale (addition).</li>
69 </ul>
70 <p class="p2"><br></p>
71 <ul>
72 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pfunc(nextFunc, resetFunc):</b> Get the stream values from a user-supplied function.</li>
73 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Pfuncn(func, repeats):</b> Get values from the function, but stop after 'repeats' items.</li>
74 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><span class="s1"><b>* </b></span><b>Proutine(routineFunc):</b> Use the function like a routine. The function should return values using .yield or .embedInStream.</li>
75 </ul>
76 <p class="p2"><br></p>
77 <p class="p2"><br></p>
78 <p class="p3"><b>Additional List Patterns</b></p>
79 <p class="p2"><br></p>
80 <ul>
81 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pser(list, repeats, offset):</b> Play through the list as many times as needed, but output only 'repeats' items.</li>
82 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pslide(list, repeats, len, step, start, wrapAtEnd):</b> Play overlapping segments from the list.</li>
83 </ul>
84 <p class="p2"><br></p>
85 <ul>
86 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pwalk(list, stepPattern, directionPattern, startPos):</b> Random walk over the list.</li>
87 </ul>
88 <p class="p2"><br></p>
89 <ul>
90 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Place(list, repeats, offset):</b> Interlace any arrays found in the main list.</li>
91 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ppatlace(list, repeats, offset):</b> Interlace any patterns found in the main list.</li>
92 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ptuple(list, repeats):</b> Collect the list items into an array as the return value.</li>
93 </ul>
94 <p class="p2"><br></p>
95 <p class="p2"><br></p>
96 <p class="p3"><b>Additional Random Number Generators</b></p>
97 <p class="p2"><br></p>
98 <ul>
99 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgbrown(lo, hi, step, length):</b> Brownian motion, geometric scale (multiplication).</li>
100 </ul>
101 <p class="p2"><br></p>
102 <ul>
103 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pbeta(lo, hi, prob1, prob2, length):</b> Beta distribution, where prob1 = &#x03B1; (alpha) and prob2 = &#x03B2; (beta).</li>
104 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pcauchy(mean, spread, length):</b> Cauchy distribution.</li>
105 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgauss(mean, dev, length):</b> Guassian (normal) distribution.</li>
106 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Phprand(lo, hi, length):</b> Returns the greater of two equal-distribution random numbers.</li>
107 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Plprand(lo, hi, length):</b> Returns the lesser of two equal-distribution random numbers.</li>
108 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pmeanrand(lo, hi, length):</b> Returns the average of two equal-distribution random numbers, i.e., (x +<span class="Apple-converted-space">  </span>y) / 2.</li>
109 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ppoisson(mean, length):</b> Poisson distribution.</li>
110 </ul>
111 <p class="p2"><br></p>
112 <ul>
113 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pprob(distribution, lo, hi, length, tableSize):</b> Arbitrary distribution, based on a probability table.</li>
114 </ul>
115 <p class="p2"><br></p>
116 <p class="p2"><br></p>
117 <p class="p4"><b>Functional descriptions of patterns</b></p>
118 <p class="p2"><br></p>
119 <p class="p5"><b>List Patterns</b></p>
120 <p class="p2"><br></p>
121 <p class="p3">The most obvious thing one would want to do with a pattern is to give it a list of values and have it read them out in order. You have a couple of choices, which differ in their handling of the 'repeats' parameter.</p>
122 <p class="p2"><br></p>
123 <p class="p3"><b>Pseq(list, repeats, offset):</b> Play through the entire list 'repeats' times.</p>
124 <p class="p3"><b>Pser(list, repeats, offset):</b> Play through the list as many times as needed, but output only 'repeats' items.</p>
125 <p class="p2"><br></p>
126 <p class="p6"><span class="s2">Pseq</span><span class="s3">(#[1, 2, 3], 4).asStream.all;<span class="Apple-tab-span"> </span></span>// 12 items = 4 repeats * 3 items</p>
127 <p class="p7"><span class="s2">Pser</span>(#[1, 2, 3], 4).asStream.all;<span class="Apple-tab-span"> </span><span class="s1">// 4 items only</span></p>
128 <p class="p2"><br></p>
129 <p class="p3"><span class="s4"><a href="../Patterns/Pseq.html">Pseq</a></span> is an obvious choice for streaming out known pitch and rhythm values.</p>
130 <p class="p2"><br></p>
131 <p class="p3">Before playing a Pbind pattern such as this, make sure the server is booted.</p>
132 <p class="p2"><br></p>
133 <p class="p8">s.boot;</p>
134 <p class="p9"><br></p>
135 <p class="p8">(</p>
136 <p class="p8">p = <span class="s5">Pbind</span>(</p>
137 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pseq</span>(#[0, 0, 4, 4, 5, 5, 4], 1),</p>
138 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, <span class="s5">Pseq</span>(#[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1], 1)</p>
139 <p class="p8">).play;</p>
140 <p class="p7">)</p>
141 <p class="p2"><br></p>
142 <p class="p3">To stop the examples in this file, use the "stop" keyboard shortcut (cmd-. on Mac OSX, alt-. on Windows, check documentation for other editors). Or:</p>
143 <p class="p2"><br></p>
144 <p class="p8">p.stop;</p>
145 <p class="p2"><br></p>
146 <p class="p3">A variation, <a href="../Patterns/Pslide.html"><span class="s2">Pslide</span></a>, plays overlapping segments of the input list.</p>
147 <p class="p2"><br></p>
148 <p class="p3"><b>Pslide(list, repeats, len, step, start, wrapAtEnd)</b></p>
149 <p class="p3">repeats: <span class="Apple-tab-span"> </span>number of segments</p>
150 <p class="p3">len:<span class="Apple-tab-span"> </span> <span class="Apple-tab-span"> </span>length of each segment</p>
151 <p class="p3">step:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>is how far to step the start of each segment from previous.</p>
152 <p class="p3">start:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>what index to start at.</p>
153 <p class="p3">wrapAtEnd: if true (default), indexing wraps around if goes past beginning or end. If false, the pattern stops if it hits a nil element or goes outside the list bounds.</p>
154 <p class="p2"><br></p>
155 <p class="p3">If step == 1, then the first segment is at 'start', the second at 'start' + 1, and so on.</p>
156 <p class="p2"><br></p>
157 <p class="p7"><span class="s2">Pslide</span>(#[1, 2, 3, 4, 5, 6, 7, 8], 10, 3, 1, 0, <span class="s2">false</span>).asStream.all;</p>
158 <p class="p10"><br></p>
159 <p class="p6">// or, to show the segments as separate arrays</p>
160 <p class="p7"><span class="s2">Pslide</span>(#[1, 2, 3, 4, 5, 6, 7, 8], 10, 3, 1, 0, <span class="s2">false</span>).clump(3).asStream.all;</p>
161 <p class="p10"><br></p>
162 <p class="p6">// Flock of Seagulls!</p>
163 <p class="p7">(</p>
164 <p class="p8">p = <span class="s5">Pbind</span>(</p>
165 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pslide</span>((-6, -4 .. 12), 8, 3, 1, 0),</p>
166 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, <span class="s5">Pseq</span>(#[0.1, 0.1, 0.2], <span class="s5">inf</span>),</p>
167 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\sustain<span class="s3">, 0.15</span></p>
168 <p class="p8">).play;</p>
169 <p class="p7">)</p>
170 <p class="p2"><br></p>
171 <p class="p3"><b>Random-order list patterns</b></p>
172 <p class="p2"><br></p>
173 <p class="p3"><b>Prand(list, repeats):</b> Choose items from the list randomly (same as list.choose).</p>
174 <p class="p2"><br></p>
175 <p class="p6">// Prand: given scale degrees (pentatonic) with equal probability of each</p>
176 <p class="p7">(</p>
177 <p class="p8">p = <span class="s5">Pbind</span>(</p>
178 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Prand</span>([0, 1, 2, 4, 5], <span class="s5">inf</span>),</p>
179 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 0.25</p>
180 <p class="p8">).play;</p>
181 <p class="p7">)</p>
182 <p class="p2"><br></p>
183 <p class="p3"><b>Pxrand(list, repeats):</b> Choose randomly, but never repeat the same item twice in immediate succession.</p>
184 <p class="p2"><br></p>
185 <p class="p6">// Pxrand: same as above but never repeats a pitch twice in a row</p>
186 <p class="p7">(</p>
187 <p class="p8">p = <span class="s5">Pbind</span>(</p>
188 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pxrand</span>([0, 1, 2, 4, 5], <span class="s5">inf</span>),</p>
189 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 0.25</p>
190 <p class="p8">).play;</p>
191 <p class="p7">)</p>
192 <p class="p2"><br></p>
193 <p class="p3"><b>Pshuf(list, repeats):</b> Shuffle the list in random order, and use the same random order 'repeats' times. Like list.scramble.</p>
194 <p class="p2"><br></p>
195 <p class="p6">// Pshuf: randomly ordered once and repeated</p>
196 <p class="p7">(</p>
197 <p class="p8">p = <span class="s5">Pbind</span>(</p>
198 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pshuf</span>([0, 1, 2, 4, 5], <span class="s5">inf</span>),</p>
199 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 0.25</p>
200 <p class="p8">).play;</p>
201 <p class="p7">)</p>
202 <p class="p2"><br></p>
203 <p class="p3"><b>Pwrand(list, weights, repeats):</b> Choose randomly, according to weighted probabilities (same as list.wchoose(weights)).</p>
204 <p class="p2"><br></p>
205 <p class="p6">// Pwrand: these probabilities favor triadic notes from scale degrees</p>
206 <p class="p7">(</p>
207 <p class="p8">p = <span class="s5">Pbind</span>(</p>
208 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pwrand</span>((0..7), [4, 1, 3, 1, 3, 2, 1].normalizeSum, <span class="s5">inf</span>),</p>
209 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 0.25</p>
210 <p class="p8">).play;</p>
211 <p class="p7">)</p>
212 <p class="p2"><br></p>
213 <p class="p3"><b>Pwalk(list, stepPattern, directionPattern, startPos):</b> Random walk over the list. This pattern is a bit more complicated; see its help file for details.</p>
214 <p class="p2"><br></p>
215 <p class="p2"><br></p>
216 <p class="p5"><b>Interlacing values and making arrays</b></p>
217 <p class="p2"><br></p>
218 <p class="p3">These are opposing operations: interlacing means splitting arrays and merging them into a stream of single values, and arrays can be made out of single-value streams as well.</p>
219 <p class="p2"><br></p>
220 <p class="p3"><b>Place(list, repeats, offset):</b> Take one from each item in the main array item in succession. Hard to explain, easier to see:</p>
221 <p class="p2"><br></p>
222 <p class="p7">Place([0, [1, 2], [3, 4, 5]], 3).asStream.all;</p>
223 <p class="p7">--&gt; [ 0, 1, 3, 0, 2, 4, 0, 1, 5 ]</p>
224 <p class="p2"><br></p>
225 <p class="p3">If we turn this into a matrix and read vertically, the original arrays are clearly visible:</p>
226 <p class="p2"><br></p>
227 <p class="p7"><span class="s2">Place</span>([0, [1, 2], [3, 4, 5]], 3).clump(3).do(<span class="s2">_</span>.postln);</p>
228 <p class="p10"><br></p>
229 <p class="p6"><span class="s3">[ 0, 1, 3 ]<span class="Apple-tab-span"> </span></span>// leftmost column: 0 from first Place item</p>
230 <p class="p6"><span class="s3">[ 0, 2, 4 ]<span class="Apple-tab-span"> </span></span>// second column: alternates between 1 and 2, from second Place item</p>
231 <p class="p6"><span class="s3">[ 0, 1, 5 ]<span class="Apple-tab-span"> </span></span>// third column: 3, 4, 5 from third Place item</p>
232 <p class="p2"><br></p>
233 <p class="p3"><b>Ppatlace(list, repeats, offset):</b> Take one value from each sub-pattern in order.</p>
234 <p class="p2"><br></p>
235 <p class="p6">// Hanon exercise</p>
236 <p class="p7">(</p>
237 <p class="p8">p = <span class="s5">Pbind</span>(</p>
238 <p class="p12"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s6">\degree</span><span class="s3">, </span>Ppatlace<span class="s3">([</span></p>
239 <p class="p13"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s5">Pseries</span><span class="s3">(0, 1, 8),<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// first, third etc. notes</p>
240 <p class="p13"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span><span class="s5">Pseries</span><span class="s3">(2, 1, 7)<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// second, fourth etc. notes</p>
241 <p class="p8"><span class="Apple-tab-span"> </span>], <span class="s5">inf</span>),</p>
242 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 0.25</p>
243 <p class="p8">).play;</p>
244 <p class="p7">)</p>
245 <p class="p2"><br></p>
246 <p class="p3">That's also a taste of things to come: Patterns can be nested.</p>
247 <p class="p2"><br></p>
248 <p class="p3"><b>Ptuple(list, repeats):</b> Get one value from each item in the array, and return all of them as an array of values.</p>
249 <p class="p2"><br></p>
250 <p class="p6">// Chords</p>
251 <p class="p6">// \degree receives [7, 9, 4], then [6, 7, 4] successively, expanded to chords on the server</p>
252 <p class="p7">(</p>
253 <p class="p8">p = <span class="s5">Pbind</span>(</p>
254 <p class="p11"><span class="s3"><span class="Apple-tab-span"> </span></span>\degree<span class="s3">, </span><span class="s5">Ptuple</span><span class="s3">([</span></p>
255 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Pseries</span>(7, -1, 8),</p>
256 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Pseq</span>([9, 7, 7, 7, 4, 4, 2, 2], 1),</p>
257 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s5">Pseq</span>([4, 4, 4, 2, 2, 0, 0, -3], 1)</p>
258 <p class="p8"><span class="Apple-tab-span"> </span>], 1),</p>
259 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, 1</p>
260 <p class="p8">).play;</p>
261 <p class="p7">)</p>
262 <p class="p2"><br></p>
263 <p class="p5"><b>Generating values</b></p>
264 <p class="p2"><br></p>
265 <p class="p3"><b>Arithmetic and geometric series</b></p>
266 <p class="p2"><br></p>
267 <p class="p3">Now, let's move to patterns that produce values mathematically, without using a predefined list.</p>
268 <p class="p2"><br></p>
269 <p class="p3"><b>Pseries(start, step, length):</b> Arithmetic series, successively adding 'step' to the starting value, returning a total of 'length' items.</p>
270 <p class="p3"><b>Pgeom(start, grow, length):</b> Geometric series, successively multiplying the current value by 'grow'.</p>
271 <p class="p2"><br></p>
272 <p class="p6">// Use Pseries for a scale and Pgeom for an accelerando</p>
273 <p class="p7">(</p>
274 <p class="p8">p = <span class="s5">Pbind</span>(</p>
275 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\degree</span>, <span class="s5">Pseries</span>(-7, 1, 15),</p>
276 <p class="p8"><span class="Apple-tab-span"> </span><span class="s6">\dur</span>, <span class="s5">Pgeom</span>(0.5, 0.89140193218427, 15)</p>
277 <p class="p8">).play;</p>
278 <p class="p7">)</p>
279 <p class="p2"><br></p>
280 <p class="p3"><b>Third-party extension alert:</b> If you want an arithmetic or geometric series to start at one number and end at another specific number, the step size/multiplier must be calculated from the endpoints and the number of items desired. The ddwPatterns quark includes a convenience method, <b>fromEndpoints</b>, for both Pseries and Pgeom that performs this calculation. It's necessary to give an exact number of repeats, at least two and less than infinity.</p>
281 <p class="p2"><br></p>
282 <p class="p6"><span class="s3">p = </span><span class="s2">Pgeom</span><span class="s3">.fromEndpoints(0.5, 0.1, 15);<span class="Apple-tab-span"> </span></span>// error if ddwPatterns not installed</p>
283 <p class="p7">p.postcs;</p>
284 <p class="p2"><br></p>
285 <p class="p3">Prints:</p>
286 <p class="p7">Pgeom(0.5, 0.89140193218427, 15)</p>
287 <p class="p2"><br></p>
288 <p class="p2"><br></p>
289 <p class="p3"><b>Random numbers and probability distributions</b></p>
290 <p class="p2"><br></p>
291 <ul>
292 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pwhite(lo, hi, length):</b> Produces 'length' random numbers with equal distribution ('white' refers to white noise).</li>
293 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pexprand(lo, hi, length):</b> Same, but the random numbers have an exponential distribution, favoring lower numbers. This is good for frequencies, and also durations (because you need more notes with a shorter duration to balance the weight of longer notes).</li>
294 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pbrown(lo, hi, step, length):</b> Brownian motion. Each value adds a random step to the previous value, where the step has an equal distribution between -step and +step.</li>
295 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgbrown(lo, hi, step, length):</b> Brownian motion on a geometric scale. Each value multiplies a random step factor to the previous value.</li>
296 </ul>
297 <p class="p2"><br></p>
298 <ul>
299 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pbeta(lo, hi, prob1, prob2, length):</b> Beta distribution, where prob1 = &#x03B1; (alpha) and prob2 = &#x03B2; (beta).</li>
300 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pcauchy(mean, spread, length):</b> Cauchy distribution.</li>
301 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pgauss(mean, dev, length):</b> Guassian (normal) distribution.</li>
302 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Phprand(lo, hi, length):</b> Returns the greater of two equal-distribution random numbers.</li>
303 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Plprand(lo, hi, length):</b> Returns the lesser of two equal-distribution random numbers.</li>
304 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pmeanrand(lo, hi, length):</b> Returns the average of two equal-distribution random numbers, i.e., (x + y) / 2.</li>
305 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Ppoisson(mean, length):</b> Poisson distribution.</li>
306 </ul>
307 <p class="p2"><br></p>
308 <ul>
309 <li style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pprob(distribution, lo, hi, length, tableSize):</b> Given an array of relative probabilities across the desired range (a histogram) representing an arbitrary distribution, generates random numbers corresponding to that distribution.</li>
310 </ul>
311 <p class="p2"><br></p>
312 <p class="p3">To see a distribution, make a histogram out of it.</p>
313 <p class="p2"><br></p>
314 <p class="p7"><span class="s2">Pmeanrand</span>(0.0, 1.0, <span class="s2">inf</span>).asStream.nextN(10000).histo(200, 0.0, 1.0).plot;</p>
315 <p class="p2"><br></p>
316 <p class="p2"><br></p>
317 <p class="p5"><b>Catchall Patterns</b></p>
318 <p class="p2"><br></p>
319 <p class="p3">Not everything is pre-written as a pattern class. These patterns let you embed custom logic.</p>
320 <p class="p2"><br></p>
321 <p class="p3"><b>Pfunc(nextFunc, resetFunc):</b> The next value is the return value from evaluating nextFunc. If .reset is called on a stream made from this pattern, resetFunc is evaluated. The stream will run indefinitely until nextFunc returns nil.</p>
322 <p class="p2"><br></p>
323 <p class="p3"><b>Pfuncn(func, repeats):</b> Like Pfunc, output values come from evaluating the function. Pfuncn, however, returns exactly 'repeats' values and then stops. The default number of repeats is 1.</p>
324 <p class="p2"><br></p>
325 <p class="p3"><b>Proutine(routineFunc):</b> Use the routineFunc in a routine. The stream's output values are whatever this function .yields. Proutine ends when it yields nil.</p>
326 <p class="p2"><br></p>
327 <p class="p2"><br></p>
328 <p class="p3">Next, we'll look at the central pattern for audio sequencing: <b>Pbind</b>.</p>
329 <p class="p2"><br></p>
330 <p class="p14"><span class="s3">Previous:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_01_Introduction.html"><span class="s7">PG_01_Introduction</span></a></span></p>
331 <p class="p14"><span class="s3">Next:<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><a href="PG_03_What_Is_Pbind.html"><span class="s7">PG_03_What_Is_Pbind</span></a></span></p>
332 </body>
333 </html>